package com.ruis.kotboot.config

import com.baomidou.mybatisplus.generator.AutoGenerator
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert
import com.baomidou.mybatisplus.generator.config.po.TableInfo
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType
import com.baomidou.mybatisplus.generator.config.rules.DbType
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
import java.io.File

/**
 * Created by mgr on 2018/2/14.
 */


/**
 * 代码生成器
 * @author mgr
 * @date 2018/1/24
 * @url https://monux.win
 */
fun main(args: Array<String>) {
    val isKotlin = true
    val author = "LinyoRuisMonux"
    val pkgType = "mysql"

    val mpg = AutoGenerator()

    val path = File("kotboot-api").absolutePath

    // 全局配置
    mpg.globalConfig = GlobalConfig().also {
        it.outputDir = if (isKotlin) {
            "$path/src/main/kotlin"
        } else {
            "$path/src/main/java"
        }
        it.isFileOverride = true
        it.isActiveRecord = false
        it.isEnableCache = false
        it.isOpen = false
        it.author = author
        it.isKotlin = isKotlin
        it.isBaseResultMap = true
        it.isBaseColumnList = false
        it.mapperName = "%sMapper"
        it.xmlName = "%sMapper"
        it.serviceName = "%sService"
//        it.serviceImplName = "%sServiceImpl"
    }

    // 数据源
    mpg.dataSource = DataSourceConfig().also {
        it.dbType = DbType.MYSQL
        it.typeConvert = object : MySqlTypeConvert() {
            override fun processTypeConvert(fieldType: String?): DbColumnType {
                return super.processTypeConvert(fieldType)
            }
        }
        it.driverName = "com.mysql.jdbc.Driver"
//        it.username = "archives"
//        it.password = "archives"
//        it.url = "jdbc:mysql://192.168.1.124:3307/archives_sys?characterEncoding=utf8"
        it.url = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"
        it.username = "root"
        it.password = "123456"
    }

    // 策略配置
    mpg.strategy = StrategyConfig().also {
        it.setDbColumnUnderline(true)
        it.isCapitalMode = false
        it.naming = NamingStrategy.underline_to_camel
        //            setTablePrefix()
        it.superEntityClass = "com.ruis.kotboot.common.base.RuisModel"
        it.setSuperEntityColumns("id", "create_by", "create_date", "update_by", "update_date", "remarks", "enable")
        it.superMapperClass = "com.ruis.kotboot.common.base.RuisMapper"
        it.superServiceClass = "com.ruis.kotboot.common.base.RuisService"
//        it.superServiceImplClass = "com.ruis.kotboot.common.base.RuisServiceImpl"
//            superControllerClass = ""
//        setInclude("user")
        it.isEntityBooleanColumnRemoveIsPrefix = true
        it.logicDeleteFieldName = "enable"
//            setExclude("")
    }

    // 包配置
    mpg.packageInfo = PackageConfig().also {
        it.parent = "com.ruis.kotboot.api.$pkgType"
        it.entity = "model"
    }


    val cfg = object : InjectionConfig() {
        override fun initMap() {
        }
    }.also {
        it.fileOutConfigList = ArrayList<FileOutConfig>().also {
            it.add(object : FileOutConfig("/templates/mapper.xml.vm") {
                override fun outputFile(tableInfo: TableInfo?): String {
                    return "$path/src/main/resources/mappings/${pkgType.replace(".", "/")}/${tableInfo?.mapperName}.xml"
                }
            })
        }
    }

    mpg.cfg = cfg

    mpg.template = TemplateConfig().also {
        it.controller = null // 不生成controller
        it.xml = null
        it.serviceImpl = null
    }

    mpg.execute()
}
